﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Marmat.Forms
{
    /// <summary>
    /// 
    /// </summary>
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    public class GridControlDesigner : System.Windows.Forms.Design.ControlDesigner
    {
        /// <summary>
        /// 为 System.ComponentModel.Design.DesignSurfaceManager.ActiveDesignSurfaceChanged事件提供数据。
        /// </summary>
        public override DesignerVerbCollection Verbs
        {
            get
            {
                DesignerVerbCollection verbCollection = new DesignerVerbCollection();
                verbCollection.Add(new DesignerVerb("生成绑定控件", this.CreateControlCollection));
                verbCollection.Add(new DesignerVerb("创建列集合", this.CreateColumnCollection));
                return verbCollection;
            }
        }

        private void CreateControlCollection(object sender, EventArgs e)
        {
            try
            {
                using (BindingFieldDesigner designer = new BindingFieldDesigner())
                {
                    IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost));
                    designer.CreateComponent(host, this.Component as IBindingSource);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "异常", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
        }

        private void CreateColumnCollection(object sender, EventArgs e)
        {
            try
            {
                Marmat.Forms.Design.DesignSupport.InitializeService(this);
                IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost));
                System.Windows.Forms.Form form = host.RootComponent as System.Windows.Forms.Form;
                Marmat.Forms.GridControl gridControl = this.Control as Marmat.Forms.GridControl;
                Marmat.Design.GridControl.GridControlEntityMemberForm frm = new Marmat.Design.GridControl.GridControlEntityMemberForm(this.Control);
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    IComponentChangeService c = (IComponentChangeService)this.GetService(typeof(IComponentChangeService));
                    DesignerTransaction tran = host.CreateTransaction();//创建事务 
                    gridControl.Columns.Clear();
                    foreach (var row in frm.dataGrid1.Select("Check = 1"))
                    {
                        Marmat.Forms.Views.Grid.Column column = new Marmat.Forms.Views.Grid.Column();
                        column.Name = (string)row["Name"];
                        column.FieldName = column.Name;
                        column.Caption = (string)row["Caption"];
                        column.DataType = (Type)row["DataType"];
                        gridControl.Columns.Add(column);
                    }
                    c.OnComponentChanged(form, null, null, null);//通知窗体修改服务已完成 
                    tran.Commit();//提交事务 
                }
                //Marmat.Design.GridControl.Form1 frm = new Marmat.Design.GridControl.Form1(form, this.Control);
                //if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                //{
                //    if (frm.comboBox1.SelectedItem is Marmat.Forms.IBindingSource bindingSource)
                //    {
                //        gridControl.Columns.Clear();
                //        var columnCollection = bindingSource.GetColumnCollection();
                //        foreach (var item in columnCollection)
                //        {
                //            Marmat.Forms.Views.Grid.Column column = new Marmat.Forms.Views.Grid.Column();
                //            column.Name = item.Name;
                //            column.FieldName = item.Name;
                //            column.Caption = item.Caption;
                //            gridControl.Columns.Add(column);
                //        }
                //    }
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "异常", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
        }
    }
}
